<?php

namespace app\home\model;

use Think\Db;
use think\Model;

class User extends Model
{
    public function getUserInfo($user){
        return Db::table('sg_user')->field('password,nickname,id')->where(['email'=>$user])->find();

    }
    public function UserAdd($nickname,$email,$pwd,$tel){
        $map['nickname'] = $nickname;
        $map['email'] = $email;
        $map['tel'] = $tel;
        preg_match(config('EMAIL_PREG_MATCH'),$map['email'],$match);
        if(empty($match)){
            return['msg'=>'邮箱格式错误!','status'=>'-1'];

        }
        preg_match(config('MOBILE_PREG_MATCH'),$map['tel'],$match);
        if(empty($match)){
            return['msg'=>'手机格式错误!','status'=>'-1'];

        }
        $a = Db::table('sg_user')->whereor($map)->find();
        if (!empty($a)){
            if ($a['email'] == $email){
                return ['msg'=>'邮箱已存在','status'=>'-1'];
            }
            if ($a['nickname'] == $nickname){
                return ['msg'=>'用户名已存在','status'=>'-1'];
            }
            if ($a['tel'] == $tel){
                return ['msg'=>'手机号已存在','status'=>'-1'];
            }
        }
        $map['password'] = $pwd;
        $b = Db::table('sg_user')->insert($map);
        if($b){
            return ['msg'=>'添加成功','status'=>'1'];
        }
        return ['msg'=>'添加失败','status'=>'-1'];
    }
    public function siteAdd($data){

        return Db::table('sg_site')->insert($data);
    }
    public function siteInfo($uid){
        return Db::table('sg_site')->where('uid',$uid)->select();
    }
    public function editInfo($id){
        return Db::table('sg_site')->where('id',$id)->find();
    }
    public function siteEdit($id,$data){
        return Db::table('sg_site')->where('id',$id)->update($data);
    }
    public function siteDel($id){
        return Db::table('sg_site')->where('id',$id)->delete();
    }

}
